package com.library.admin.modules.rolemenu.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.library.admin.modules.role.entity.Role;
import com.library.admin.modules.role.service.RoleService;
import com.library.admin.modules.rolemenu.entity.RoleMenu;
import com.library.admin.modules.rolemenu.vo.RoleMenuVO;
import com.library.admin.modules.rolemenu.bo.RoleMenuPage;
import com.library.admin.modules.rolemenu.bo.RoleMenuInsert;
import com.library.admin.modules.rolemenu.bo.RoleMenuUpdate;
import com.library.admin.modules.rolemenu.mapper.RoleMenuMapper;
import com.library.admin.modules.rolemenu.struct.RoleMenuStructMapper;
import com.library.admin.modules.rolemenu.service.RoleMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import com.library.common.util.PageCovertUtil;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * @Author xyh
 * @Desc 角色和菜单关联表(RoleMenu)表服务实现类
 * @Date 2023-10-02 15:09:33
 */

@Service("roleMenuService")
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Resource
    private RoleMenuStructMapper roleMenuStructMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;


    @Override
    public IPage<RoleMenuVO> queryByPage(RoleMenuPage page) {
        // 查询条件
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        // 查询分页数据
        Page<RoleMenu> roleMenuPage = new Page<RoleMenu>(page.getCurrent(), page.getSize());
        IPage<RoleMenu> pageData = baseMapper.selectPage(roleMenuPage, queryWrapper);

        //转换成vo
        IPage<RoleMenuVO> records = PageCovertUtil.pageVoCovert(pageData, RoleMenuVO.class);
        return records;
    }


    @Override
    public RoleMenuVO queryById(Integer id) {
        RoleMenu roleMenu = baseMapper.selectById(id);
        return roleMenuStructMapper.roleMenuToRoleMenuVO(roleMenu);
    }

    @Override
    public boolean insert(RoleMenuInsert roleMenuInsert) {
        //先删除之前的关系，再添加新的角色和菜单的关系
        remove(new QueryWrapper<RoleMenu>().lambda().eq(RoleMenu::getRoleId, roleMenuInsert.getRoleId()));
        //重新绑定
        if (CollUtil.isNotEmpty(roleMenuInsert.getMenuIds())) {
            roleMenuInsert.getMenuIds().forEach(rm -> {
                RoleMenu roleMenu = new RoleMenu(roleMenuInsert.getRoleId(), rm);
                save(roleMenu);
            });
        }
        return true;
    }


    @Override
    public boolean update(RoleMenuUpdate roleMenuUpdate) {
        RoleMenu roleMenu = roleMenuStructMapper.updateToRoleMenu(roleMenuUpdate);
        updateById(roleMenu);
        return true;
    }

    @Override
    public boolean deleteById(Integer id) {
        return removeById(id);
    }

    @Override
    public Set<Integer> getMenuIdsByRoleIds(Collection<Integer> roleIds) {
        Set<Integer> ret = new HashSet<>(roleIds.size() << 4);
        for (Integer roleId : roleIds) {
            ret.addAll(
                    this.list(
                            new QueryWrapper<RoleMenu>()
                                    .lambda()
                                    .select(RoleMenu::getMenuId)
                                    .eq(RoleMenu::getRoleId, roleId)
                    ).stream().map(RoleMenu::getMenuId).collect(Collectors.toSet()));
        }
        return ret;
    }

}
